import book.Library;
import com.bit.utils.FileUtils;
import user.ProxyUser;
import user.User;
import user.factory.AdminUserFactory;
import user.factory.IUserFactory;
import user.factory.NormalUserFactory;

import java.util.Scanner;

public class librarySystem {
    public static void main(String[] args) {

        //工厂模式（代码不会重复，创建对象的过程 封装到了工厂当中；使得创建对象的过程和使用对象的过程分离，更加解耦）
        IUserFactory adminUserFactory = new AdminUserFactory();
        User admin = adminUserFactory.createUser("浩存",1);

        IUserFactory normalUserFactory = new NormalUserFactory();
        User normal1 = normalUserFactory.createUser("珍妮",2);
        User normal2 = normalUserFactory.createUser("智秀",3);


        //代理模式（使用代理模式来管理权限、控制对象的访问）
        ProxyUser proxyUserAdmin = new ProxyUser(admin);
        ProxyUser proxyUserNormal1 = new ProxyUser(normal1);
        ProxyUser proxyUserNormal2 = new ProxyUser(normal2);

        librarySystem librarySystem = new librarySystem();
        //选择对应角色进行登录
        ProxyUser currentUser = librarySystem.selectProxyRole(proxyUserAdmin,proxyUserNormal1,proxyUserNormal2);


        while (true) {
            int choice = currentUser.display();
            //int choice = currentUser.getRealUser().display();

            //此时无需关心是 管理员还是普通用户，代理类会做权限判断
            //选择几（choice） 就执行 哪个用户（currentUser）的哪个操作
            currentUser.handleOperation(choice);
        }

    }


    private ProxyUser selectProxyRole(ProxyUser proxyUserAdmin, ProxyUser proxyUserNormal1, ProxyUser proxyUserNormal2) {
        System.out.println("选择角色进行登录：");
        System.out.println("1.管理员\n2.普通用户(珍妮)\n3.普通用户(智秀)\n4.退出系统");
        ProxyUser currentUser = null;

        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                currentUser = proxyUserAdmin;
                break;
            case 2:
                currentUser = proxyUserNormal1;
                break;
            case 3:
                currentUser = proxyUserNormal2;
                break;
            case 4:
                System.exit(0);
                System.out.println("系统已退出...");
                break;
            default:
                break;
        }
        return currentUser;
    }
}
